import { createStore } from 'vuex'
import axios from 'axios'


const store = createStore({
    state: {
        table: [],
        domains:[],
        dirs:[],
    },
    actions: {
        async getTableData({ commit }, payload = {}) {
            try {
                const { beginTime = '', endTime = '', event = '', list = [] } = payload;
                const response = await axios.get("https://api.wotai.org/index.php/api/query/getuserinfo", {
                    params: {
                        limit: 5000,
                        beginTime: beginTime || undefined,
                        endTime: endTime || undefined,
                        event: event || undefined,
                        list: list.length > 0 ? list.join(',') : undefined,
                    }
                });
                commit('GETTABLEDATA', response.data.data);
            } catch (error) {
                console.error("Error fetching table data:", error);
            }
        },            
        // 所有域名
        async getAllDomains({ commit }) {
            try {
                const response = await axios.get("https://api.wotai.org/index.php/api/query/domain", {
                    params: {
                        auth: 'wotai'
                    }
                });
                commit('GETALLDOMAINS', response.data.data);
            } catch (error) {
                console.error("Error fetching domains data:", error);
            }
        },
        // dirs
        async getDirs({ commit }) {
            try {
                const response = await axios.get("https://api.wotai.org/index.php/api/query/getdirs", {
                    params: {
                        auth: 'wotai'
                    }
                });
                if(response.data.code === 1){
                    commit('GETDIRS', response.data.data);
                }
                return response; // 确保返回 Promise
            } catch (error) {
                console.error("Error fetching directories data:", error);
                throw error; // 确保错误也被返回
            }
        }
        
    },
    mutations: {
        GETTABLEDATA(state, value) {
            state.table = value
        },
        GETALLDOMAINS(state,value){
            state.domains = value
        },
        GETDIRS(state, value){
            state.dirs = value
        }
    },
    getters:{
        tablelength(state){
            return state.table.length
        }
    }
})

export default store
